﻿using System;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Services;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class CategoryBoxUserControl : BaseNopFrontendUserControl
    {
        private Category _category;
        private string _cssClass;

        private IPictureService _pictureService;
        private ISettingService _settingService;

        public CategoryBoxUserControl() {
            _pictureService = IoC.Resolve<IPictureService>();
            _settingService = IoC.Resolve<ISettingService>();
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(CategoryBoxUserControl_Load);
        }

        void CategoryBoxUserControl_Load(object sender, EventArgs e) {
            if (CurrentCategory != null) {
                if (!string.IsNullOrWhiteSpace(CssClass)) {
                    pnlCategory.CssClass += " " + CssClass;
                }

                string categoryURL = SEOHelper.GetCategoryUrl(CurrentCategory);

                hlCategory.NavigateUrl = categoryURL;
                hlCategory.ToolTip = String.Format(GetLocaleResourceString("Media.Category.ImageLinkTitleFormat"), CurrentCategory.LocalizedName);
                hlCategory.Text = Server.HtmlEncode(CurrentCategory.LocalizedName);

                hlImageLink.ImageUrl = _pictureService.GetPictureUrl(CurrentCategory.PictureId, _settingService.GetSettingValueInteger("Media.Category.ThumbnailImageSize", 125), true);
                hlImageLink.Width = Unit.Pixel(125);
                hlImageLink.Height = Unit.Pixel(125);
                hlImageLink.NavigateUrl = categoryURL;
                hlImageLink.ToolTip = String.Format(GetLocaleResourceString("Media.Category.ImageLinkTitleFormat"), CurrentCategory.LocalizedName);
                hlImageLink.Text = String.Format(GetLocaleResourceString("Media.Category.ImageAlternateTextFormat"), CurrentCategory.LocalizedName);
            }
        }

        public string CssClass {
            get { return _cssClass; }
            set { _cssClass = value; }
        }

        public Category CurrentCategory {
            get {
                return _category;
            }
            set {
                _category = value;
            }
        }
    }
}